home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Macintosh Sample Code / SC.003.SillyBalls / SillyBalls.p < prev    next >
Encoding:
Text File  |  1992-06-04  |  8.4 KB  |  248 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.p    -    Pascal Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.00                8/88
  15. #                1.01                6/92
  16. #
  17. #    Components:    SillyBalls.p        August 1, 1988
  18. #                SillyBalls.c        August 1, 1988
  19. #                PSillyBalls.make    August 1, 1988
  20. #                CSillyBalls.make    August 1, 1988
  21. #                TCSillyBalls.π        June 4, 1992
  22. #
  23. #    This is a very simple sample program that demonstrates how to use Color 
  24. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  25. #    a color window and draw randomly colored ovals in the window.
  26. #    
  27. #    The purpose is to show how to get some initial results with Color QuickDraw.
  28. #    It is a complete program and is very short to be as clear as possible.
  29. #    
  30. #    It does not have an Event Loop.  It is not fully functional in the sense that
  31. #    it does not do all the things you would expect a well behaved Macintosh 
  32. #    program to do, like size the window naturally, have an event loop, use menus, 
  33. #    etc.
  34. #
  35. #    See Sample and TESample for the general structure and MultiFinder techniques that
  36. #    we recommend that you use when building a new application.
  37. #
  38. ------------------------------------------------------------------------------}
  39.  
  40. Program SillyBalls;
  41. (*
  42.     Version 1.0: 6/2/88
  43.     
  44.     purpose        To demonstrate a simple color App using Color QuickDraw.
  45.                         It draws colored balls in a color window, then uses colored
  46.                         text inverted in the ball.  The ball location and color is Random.
  47.                         
  48.                         This program was written by Bo3b Johnson, 1/88.
  49.                         
  50.                         The inverted Bob text was a Skippy Blair special concept,
  51.                         kept for obvious aesthetic reasons.
  52.                         
  53.                         You can build the program with this junk:
  54. pascal SillyBalls.p
  55. Link SillyBalls.p.o ∂
  56.     "{MPW}Libraries:"Interface.o ∂
  57.     "{MPW}Libraries:"Runtime.o ∂
  58.     "{MPW}PLibraries:"Paslib.o ∂
  59.     -o SillyBalls
  60. SillyBalls
  61. *)
  62.  
  63. { Where does it fit:
  64.     This is a series of sample programs for those doing development
  65.     using Color QuickDraw.  Since the whole color problem depends
  66.     upon the exact effect desired, there are a number of answers
  67.     to how to use colors, from the simple to the radically complex.
  68.     These programs try to cover the gamut, so you should use 
  69.     which ever seems appropriate.  In most cases, use the simplest
  70.     one that will give the desired results.  The compatibility
  71.     rating is from 0..9 where low is better.  The more known risks 
  72.     there are the higher the rating.
  73.     
  74.     
  75.     The programs (in order of compatibility):
  76.     
  77.         SillyBalls:    (***)
  78.             This is the simplest use of Color QuickDraw, and does
  79.             not use the Palette Manager.  It draws randomly colored
  80.             balls in a color window.  This is intended to give you
  81.             the absolute minimum required to get color on the screen.
  82.             Written in straight Pascal code.
  83.             Compatibility rating = 0, no known risks.
  84.         
  85.         FracAppPalette:
  86.             This is a version of FracApp that uses only the Palette
  87.             Manager.  It does not support color table animation
  88.             since that part of the Palette Manager is not sufficient.
  89.             The program demonstrates a full color palette that is
  90.             used to display the Mandelbrot set.  It uses an offscreen
  91.             gDevice w/ Port to handle the data, using CopyBits to
  92.             draw to the window.  The Palette is automatically 
  93.             associated with each window.  The PICT files are read
  94.             and written using the bottlenecks, to save on memory
  95.             useage.
  96.             Written in MacApp Object Pascal code.
  97.             Compatibility rating = 0, no known risks.
  98.         
  99.         TubeTest:
  100.             This is a small demo program that demonstrates using the
  101.             Palette Manager for color table animation.  It uses a 
  102.             color palette with animating entries, and draws using the
  103.             Palette Manager.  There are two circles of animating colors
  104.             which gives a flowing tube effect.  This is a valid case
  105.             for using the animating colors aspect of the Palette Manager,
  106.             since the image is being drawn directly.
  107.             Written in straight Pascal code.
  108.             Compatibility rating = 0, no known risks.
  109.         
  110.         FracApp:
  111.             This is the ‘commercial quality’ version of FracApp.  This
  112.             version supports color table animation, using an offscreen
  113.             gDevice w/ Port, and handles multiple documents.  The
  114.             CopyBits updates to the screen are as fast as possible.  The
  115.             program does not use the Palette Manager, except to
  116.             provide for the system palette, or color modes with less than
  117.             255 colors.  For color table animation using an offscreen
  118.             gDevice w/ Port, it uses the Color Manager and handles the
  119.             colors itself.  Strict compatibility was relaxed to allow for
  120.             a higher performance program.  This is the most ‘real’ of the
  121.             sample programs.
  122.             Written in MacApp Object Pascal code.
  123.             Compatibility rating = 2.  (nothing will break, but it may not
  124.             always look correct.)
  125.         
  126.         FracApp300:
  127.             This doesn't support colors, but demonstrates how to create and
  128.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  129.             resolution on LaserWriters, and clipped on other printers (but
  130.             they still print).  It demonstrates how to use a high resolution
  131.             image as a PICT file, and how to print them out.
  132.             Written in MacApp Object Pascal code.
  133.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  134.             out of the ordinary, although supported.)
  135. }
  136.  
  137. USES    MemTypes, QuickDraw, OSIntf, ToolIntf;
  138.  
  139. CONST
  140.     BallWidth    = 20;
  141.     BallHeight    = 20;
  142.     BobSize        = 8;        { Size of text in each ball. }
  143.  
  144. VAR
  145.     windRect    : Rect;
  146.     
  147.     
  148. { Initialize everything for the program, make sure we can run. }
  149.  
  150. Procedure Initialize;
  151.  
  152. VAR
  153.     mainPtr        : WindowPtr;
  154.     error        : OSErr;
  155.     theWorld    : SysEnvRec;
  156.     
  157. BEGIN
  158.     { Test the computer to be sure we can do color.  If not we would crash, which
  159.     would be bad.  If we can’t run, just beep and exit. }
  160.     error := SysEnvirons(1, theWorld);
  161.     IF NOT theWorld.hasColorQD THEN BEGIN
  162.         SysBeep (50);
  163.         ExitToShell;                        { If no color QD, we must leave. }
  164.     END;
  165.     
  166.     { Initialize all the needed managers. }
  167.     InitGraf(@thePort);
  168.     InitFonts;
  169.     InitWindows;
  170.     InitMenus;
  171.     TEInit;
  172.     InitDialogs(NIL);
  173.     InitCursor;
  174.  
  175.     { To make the Random sequences truly random, we need to make the seed start
  176.     at a different number.  An easy way to do this is to put the current time
  177.     and date into the seed.  Since it is always incrementing the starting seed
  178.     will always be different.  Don’t for each call of Random, or the sequence
  179.     will no longer be random.  Only needed once, here in the init. }
  180.     GetDateTime (RandSeed);
  181.  
  182.     { Make a new window for drawing in, and it must be a color window.  The window is
  183.     full screen size, made smaller to make it more visible. }
  184.     windRect := screenBits.bounds;
  185.     InsetRect (windRect, 50, 50);
  186.     MainPtr := NewCWindow(NIL, windRect, 'Bo3b Land', TRUE, documentProc, 
  187.                             Pointer(-1), FALSE, 0);
  188.         
  189.     SetPort(MainPtr);                        { set window to current graf port   }
  190.     TextSize(BobSize);                        { smaller font for drawing. }
  191. END;    { Initialize }
  192.  
  193.  
  194. { NewBall: make another ball in the window at a random location and color. }
  195.  
  196. Procedure NewBall;
  197.  
  198. VAR
  199.     ballColor    : RGBColor;
  200.     ballRect    : Rect;
  201.     newLeft,
  202.     newTop        : LongInt;
  203.     
  204. BEGIN
  205.     { Make a random new color for the ball. }
  206.     WITH ballColor        DO BEGIN
  207.         red := Random;   green := Random;  blue := Random;
  208.     END;
  209.     
  210.     { Set that color as the new color to use in drawing. }
  211.     RGBForeColor (ballColor);
  212.  
  213.     { Make a Random new location for the ball, that is normalized to the window size.  
  214.     This makes the Integer from Random into a number that is 0..windRect.bottom
  215.     and 0..windRect.right.  They are normalized so that we don't spend most of our
  216.     time drawing in places outside of the window. }
  217.     newTop := Random;    newLeft := Random;
  218.     newTop := ((newTop+32767) * windRect.bottom) DIV 65536;
  219.     newLeft := ((newLeft+32767) * windRect.right) DIV 65536;
  220.     SetRect(ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  221.     
  222.     { Move pen to the new location, and paint the colored ball. }
  223.     MoveTo(newLeft, newTop);
  224.     PaintOval (ballRect);
  225.     
  226.     { Move the pen to the middle of the new ball position, for the text }
  227.     MoveTo(ballRect.Left + BallWidth DIV 2 - BobSize, 
  228.             ballRect.Top + BallHeight DIV 2 + BobSize DIV 2 -1);
  229.     
  230.     { Invert the color and draw the text there.  This won’t look quite right in 1 bit
  231.     mode, since the foreground and background colors will be the same.  Color
  232.     QuickDraw special cases this to not invert the color, to avoid invisible
  233.     drawing. }
  234.     InvertColor(ballColor); 
  235.     RGBForeColor(ballColor);
  236.     DrawString('Bob');
  237. END;    { NewBall }
  238.  
  239.  
  240. BEGIN       { Main body of program SillyBalls }
  241.     Initialize;
  242.     
  243.     Repeat
  244.         NewBall;
  245.     Until Button;
  246.     
  247. END.    { SillyBalls }
  248.